<?php
	class Admincp_Model_Slide extends Zend_Db_Table_Abstract
	{
		protected $_name = 'slide';
		protected $_primary = 'id';
		protected $_db;
		
		public function __construct()
		{
			$this->_db = Zend_Registry::get('db');
		}
        
        public function listItem()
        {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->order('id DESC');
            return $sql;
        }
        
        public function getItem($id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name)
                            ->where('id = ?', $id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function saveItem($data, $option)
        {
            if($option == 'create') {
                $this->_db->insert($this->_name, $data);
            }
            else if($option == 'update') {
                $where = $this->getAdapter()->quoteInto('id = ?', $data['id']);
                $this->_db->update($this->_name, $data, $where);
            }
        }
        
        public function getImageById($id)
        {
            $sql = $this->_db->select()
                            ->from($this->_name, array('image'))
                            ->where('id = ?', $id);
            $result = $this->_db->fetchRow($sql);
            return $result;
        }
        
        public function deleteItem($id)
        {
            $id = (int) $id;
            $result = $this->getImageById($id);
            if(is_file(PUBLIC_PATH . '/files/slide/' . $result['image'])) {
                @unlink(PUBLIC_PATH . '/files/slide/' . $result['image']);
            }
		    $where = $this->getAdapter()->quoteInto('id = ?', $id);
		    $this->_db->delete($this->_name, $where);
        }
        
        public function deleteMultiItem($listId)
        {
            foreach($listId as $value) {
                $this->deleteItem($value);
            }
        }
    }
